package tmall.portal.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import tmall.portal.eobj.UserEobj;
import tmall.security.dict.SecurityExceptionDict;
import tmall.security.exception.TSecurityException;

public class TokenUtil {

    public static UserEobj getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || authentication.getPrincipal() == null) {
            throw new TSecurityException(SecurityExceptionDict.AUTH_FAILED);
        }
        return (UserEobj) authentication.getPrincipal();
    }

    public static String getUserId() {
        UserEobj userEObj = getUser();
        return userEObj == null ? null : userEObj.getUserId();
    }

}
